/**
 * 海淀区基金爬虫
 *  
 */
import {Base,Item} from '../base';
class hdqcg extends Base{
    document:Document;
    constructor(){
        super();
        this.name = "中共北京海淀区委员会网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "hyt";
        this.collectionShowName = "会议通知";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        let url=null;
        if(pageIndex==0){
           url='http://hdqw.bjhd.gov.cn/qwyw/tzgg/index.htm';
        }else{
          url=`http://hdqw.bjhd.gov.cn/qwyw/tzgg/index_${pageIndex}.htm`;
        }
        let result :Item[]= [];
        let content=this.spider.getSync(url);
        let $=this.cheerio.load(content);
        let that =this;
        $('.secList li').each(function(index,ele){
            let item =that.buildItem();
            item.title=$(ele).find('a').text().trim();
            item.source=that.name;
            (<any>item)['会议时间']=$(ele).find('b').text().trim();
            let uri='http://hdqw.bjhd.gov.cn/qwyw/tzgg/'+$(ele).find('a').attr('href').substring(2);
            (<any>item).url='http://hdqw.bjhd.gov.cn/qwyw/tzgg/'+$(ele).find('a').attr('href').substring(2);
            result=that.getExtraItem(uri,item,result);
        });
        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        (<any>item).content =$('.content').html(); 
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new hdqcg();